package com.edp.wms.basedata.controller;

import com.edp.wms.basedata.domain.GoodsPackage;
import com.edp.wms.basedata.service.GoodsPackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basedata/goods-package")
@RequiredArgsConstructor
@Api(tags = "货品包装管理")
public class GoodsPackageController {

    private final GoodsPackageService goodsPackageService;

    /**
     * 获取所有货品包装列表
     */
    @GetMapping
    @ApiOperation("获取货品包装列表")
    public ResponseEntity<List<GoodsPackage>> getAllPackages() {
        List<GoodsPackage> packages = goodsPackageService.list();
        return ResponseEntity.ok(packages);
    }

    /**
     * 根据ID获取货品包装详情
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID获取货品包装详情")
    public ResponseEntity<GoodsPackage> getPackageById(@PathVariable Long id) {
        GoodsPackage goodsPackage = goodsPackageService.getById(id);
        if (goodsPackage != null) {
            return ResponseEntity.ok(goodsPackage);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 创建新货品包装
     */
    @PostMapping
    @ApiOperation("创建货品包装")
    public ResponseEntity<GoodsPackage> createPackage(@RequestBody GoodsPackage goodsPackage) {
        // 检查必要字段
        if (goodsPackage.getPackageCode() == null || goodsPackage.getPackageName() == null) {
            return ResponseEntity.badRequest().build();
        }

        // 设置默认状态
        if (goodsPackage.getStatus() == null) {
            goodsPackage.setStatus("0");
        }

        boolean saved = goodsPackageService.save(goodsPackage);
        if (saved) {
            GoodsPackage savedPackage = goodsPackageService.getById(goodsPackage.getId());
            return ResponseEntity.status(HttpStatus.CREATED).body(savedPackage);
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 更新货品包装信息
     */
    @PutMapping("/{id}")
    @ApiOperation("更新货品包装信息")
    public ResponseEntity<GoodsPackage> updatePackage(@PathVariable Long id, @RequestBody GoodsPackage goodsPackage) {
        // 检查要更新的货品包装是否存在
        GoodsPackage existingPackage = goodsPackageService.getById(id);
        if (existingPackage == null) {
            return ResponseEntity.notFound().build();
        }

        // 设置ID确保更新正确的记录
        goodsPackage.setId(id);

        // 执行更新操作
        boolean updated = goodsPackageService.updateById(goodsPackage);
        if (updated) {
            // 返回更新后的完整对象
            GoodsPackage updatedPackage = goodsPackageService.getById(id);
            return ResponseEntity.ok(updatedPackage);
        } else {
            return ResponseEntity.internalServerError().build();
        }
    }

    /**
     * 删除货品包装
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除货品包装")
    public ResponseEntity<Void> deletePackage(@PathVariable Long id) {
        boolean deleted = goodsPackageService.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}

